package com.ruoyi.ucar.controller;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.UUID;
import com.obs.services.model.AccessControlList;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ucar.boreq.SysFileBoReq;
import com.ruoyi.ucar.boresp.OcrBrandPriceListBo;
import com.ruoyi.ucar.boresp.OcrDrivingLicenseBoResp;
import com.ruoyi.ucar.boresp.OcrVehicleRegistrationBoResp;
import com.ruoyi.ucar.boresp.VinBrandBoResq;
import com.ruoyi.ucar.common.CommonVoResp;
import com.ruoyi.ucar.service.ISysFileMainService;
import com.ruoyi.ucar.service.UCarOcrService;
import com.ruoyi.ucar.util.CommonUtil;
import com.ruoyi.ucar.util.ResponseUtil;
import com.ruoyi.ucar.voreq.FileUploadVoReq;
import com.ruoyi.ucar.voreq.VinIdentifyVehicleVoReq;
import com.ruoyi.ucar.voresp.OcrBrandPriceListVo;
import com.ruoyi.ucar.voresp.OcrDrivingLicenseVoResp;
import com.ruoyi.ucar.voresp.OcrVehicleRegistrationVoResp;
import com.ruoyi.ucar.voresp.VinBrandVoResq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ucar/ocr")
@Api(value = "车辆证件OCR识别管理",tags = "车辆证件OCR识别模块接口")
public class UcarOcrController extends BaseController {
    @Autowired
    private UCarOcrService uCarOcrService;

    @Autowired
    private ISysFileMainService sysFileMainService;

    @PostMapping("/ocrDrivingLicenseRecognition")
    @ApiOperation(value = "行驶证识别接口")
    @Anonymous
    public CommonVoResp<OcrDrivingLicenseVoResp> ocrDrivingLicenseRecognition(FileUploadVoReq fileUploadVoReq){
        CommonVoResp<OcrDrivingLicenseVoResp> commonVoResp = new CommonVoResp<>();
        MultipartFile file=fileUploadVoReq.getFile();
        String fileOldName=file.getOriginalFilename();
        String fileNewName= UUID.randomUUID().toString();
        String fileName = fileNewName+fileOldName;
        InputStream inputStream;
        try {
            inputStream=file.getInputStream();
            OcrDrivingLicenseBoResp ocrDrivingLicenseBoResp=uCarOcrService.ocrDrivingLicenseRecognition(inputStream);
            logger.info("行驶证识别接口返回结果：{}",ocrDrivingLicenseBoResp);
            if(ocrDrivingLicenseBoResp.getErrorMsg()!=null){
                commonVoResp = ResponseUtil.setCommonVoRespOcrError(commonVoResp);
                return commonVoResp;
            }else{
                inputStream=file.getInputStream();
                //            obs上传文件
                String fileURl= sysFileMainService.obsUploadFile(fileName,inputStream, AccessControlList.REST_CANNED_PUBLIC_READ);
//         数据库保存文件数据
                SysFileBoReq sysFileBoReq=new SysFileBoReq();
                sysFileBoReq.setFileOldName(fileOldName);
                sysFileBoReq.setFileNewName(fileName);
                sysFileBoReq.setFileUrl(fileURl);
                sysFileBoReq.setSort(0l);
                sysFileBoReq.setFileSize(new Long(file.getSize()).toString());
                sysFileBoReq.setFileFormat(FileNameUtil.extName(fileOldName));
                sysFileBoReq.setFileType("drivingLicense");
                String sysFileMainUUID= sysFileMainService.saveSysFile(sysFileBoReq);
                OcrDrivingLicenseVoResp ocrDrivingLicenseVoRes= CommonUtil.objToObj(ocrDrivingLicenseBoResp, OcrDrivingLicenseVoResp.class);
                ocrDrivingLicenseVoRes.setFileUuid(sysFileMainUUID);
                if(StringUtils.isNotNull(ocrDrivingLicenseBoResp.getOcrBrandPriceListBos())&&ocrDrivingLicenseBoResp.getOcrBrandPriceListBos().size()>0){
                    List<OcrBrandPriceListVo> ocrBrandPriceListVoList=new ArrayList<>();
                    for (OcrBrandPriceListBo ocrBrandPriceListBo:ocrDrivingLicenseBoResp.getOcrBrandPriceListBos()){
                        OcrBrandPriceListVo  ocrBrandPriceListVo=CommonUtil.objToObj(ocrBrandPriceListBo,OcrBrandPriceListVo.class);
                        ocrBrandPriceListVoList.add(ocrBrandPriceListVo);
                    }
                    ocrDrivingLicenseVoRes.setOcrBrandPriceListVoList(ocrBrandPriceListVoList);
                }
                commonVoResp.setData(ocrDrivingLicenseVoRes);
            }

        } catch (IOException e) {
            commonVoResp = ResponseUtil.setCommonVoRespOcrError(commonVoResp);
            return commonVoResp;
        }

        return commonVoResp;
    }

    @PostMapping("/vinIdentifyVehicle")
    @ApiOperation(value = "VIN码查询车型接口")
    @Anonymous
    public CommonVoResp<VinBrandVoResq> vinIdentifyVehicle(VinIdentifyVehicleVoReq vinIdentifyVehicleVoReq){
        CommonVoResp<VinBrandVoResq> commonVoResp = new CommonVoResp<>();
        if(StringUtils.isEmpty(vinIdentifyVehicleVoReq.getVin())){
            return CommonUtil.setCommonVoRespNoParam(commonVoResp);
        }
        VinBrandBoResq vinBrandBoResq=uCarOcrService.vinIdentifyVehicle(vinIdentifyVehicleVoReq.getVin());
        VinBrandVoResq vinBrandVoResq= CommonUtil.objToObj(vinBrandBoResq,VinBrandVoResq.class);
        if(StringUtils.isNotNull(vinBrandBoResq.getOcrBrandPriceListBos())&&vinBrandBoResq.getOcrBrandPriceListBos().size()>0){
            List<OcrBrandPriceListVo> ocrBrandPriceListVoList=new ArrayList<>();
            for (OcrBrandPriceListBo ocrBrandPriceListBo:vinBrandBoResq.getOcrBrandPriceListBos()){
                OcrBrandPriceListVo  ocrBrandPriceListVo=CommonUtil.objToObj(ocrBrandPriceListBo,OcrBrandPriceListVo.class);
                ocrBrandPriceListVoList.add(ocrBrandPriceListVo);
            }
            vinBrandVoResq.setOcrBrandPriceListVoList(ocrBrandPriceListVoList);
        }
        commonVoResp.setData(vinBrandVoResq);
        return commonVoResp;
    }


    @PostMapping("/ocrVehicleRegistrationRecognition")
    @ApiOperation(value = "机动车登记证书识别接口")
    @Anonymous
    public CommonVoResp<OcrVehicleRegistrationVoResp> ocrVehicleRegistrationRecognition(FileUploadVoReq fileUploadVoReq){
        CommonVoResp<OcrVehicleRegistrationVoResp> commonVoResp = new CommonVoResp<>();
        MultipartFile file=fileUploadVoReq.getFile();
        String fileOldName=file.getOriginalFilename();
        String fileNewName= UUID.randomUUID().toString();
        String fileName = fileNewName+fileOldName;
        InputStream inputStream;
        try {
            inputStream=file.getInputStream();
            OcrVehicleRegistrationBoResp ocrVehicleRegistrationBoResp=uCarOcrService.ocrVehicleRegistrationRecognition(inputStream);
            logger.info("机动车登记证书识别接口返回结果：{}",ocrVehicleRegistrationBoResp);
            if(ocrVehicleRegistrationBoResp.getErrorMsg()!=null){
                commonVoResp = ResponseUtil.setCommonVoRespOcrError(commonVoResp);
                return commonVoResp;
            }else{
                inputStream=file.getInputStream();
                //            obs上传文件
                String fileURl= sysFileMainService.obsUploadFile(fileName,inputStream,AccessControlList.REST_CANNED_PUBLIC_READ);
//         数据库保存文件数据
                SysFileBoReq sysFileBoReq=new SysFileBoReq();
                sysFileBoReq.setFileOldName(fileOldName);
                sysFileBoReq.setFileNewName(fileName);
                sysFileBoReq.setFileUrl(fileURl);
                sysFileBoReq.setSort(0l);
                sysFileBoReq.setFileSize(new Long(file.getSize()).toString());
                sysFileBoReq.setFileFormat(FileNameUtil.extName(fileOldName));
                sysFileBoReq.setFileType("vehicleRegistration");
                String sysFileMainUUID= sysFileMainService.saveSysFile(sysFileBoReq);
                OcrVehicleRegistrationVoResp ocrVehicleRegistrationVoRes= CommonUtil.objToObj(ocrVehicleRegistrationBoResp, OcrVehicleRegistrationVoResp.class);
                ocrVehicleRegistrationVoRes.setFileUuid(sysFileMainUUID);
                if(StringUtils.isNotNull(ocrVehicleRegistrationBoResp.getOcrBrandPriceListBos())&&ocrVehicleRegistrationBoResp.getOcrBrandPriceListBos().size()>0){
                    List<OcrBrandPriceListVo> ocrBrandPriceListVoList=new ArrayList<>();
                    for (OcrBrandPriceListBo ocrBrandPriceListBo:ocrVehicleRegistrationBoResp.getOcrBrandPriceListBos()){
                        OcrBrandPriceListVo  ocrBrandPriceListVo=CommonUtil.objToObj(ocrBrandPriceListBo,OcrBrandPriceListVo.class);
                        ocrBrandPriceListVoList.add(ocrBrandPriceListVo);
                    }
                    ocrVehicleRegistrationVoRes.setOcrBrandPriceListVoList(ocrBrandPriceListVoList);
                }
                commonVoResp.setData(ocrVehicleRegistrationVoRes);
            }

        } catch (IOException e) {
            commonVoResp = ResponseUtil.setCommonVoRespOcrError(commonVoResp);
            return commonVoResp;
        }

        return commonVoResp;
    }


}
